package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.AttentionDao;
import com.gdit.zax.entitys.Attention;
import com.gdit.zax.entitys.AttentionUser;
import com.gdit.zax.entitys.VideoALL;
import com.gdit.zax.utils.DBUtil;

public class AttentionDaoImpl implements AttentionDao{
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public AttentionDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}


	public boolean addAttention(int uid, int auid) {
		try {
			String sql = "insert t_attention set UserId=?,AUserId=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, auid);
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	public boolean delAttention(int uid, int auid) {
		String sql = "delete from t_attention where UserId=? and AUserId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, auid);
			int result = ps.executeUpdate();
			if (result > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	public boolean delFrans(int uid, int auid) {
		// TODO Auto-generated method stub
		return false;
	}


	public int getAttentionSum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}


	public int getFransSum(int uid) {
		// TODO Auto-generated method stub
		return 0;
	}


	public ArrayList<AttentionUser> listAttention(int uid, int page,int onepagecount) {
		ArrayList<AttentionUser> list = new ArrayList<AttentionUser>();
		String sql = "select * from t_attention as a inner join t_user as u on a.auserid=u.id where a.UserId=? limit ?,?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);
			
			
			rs = ps.executeQuery();
			
			while (rs.next()) {
				AttentionUser be = DBUtil.getJoinObject(rs, AttentionUser.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<AttentionUser> listAttention(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}


	public ArrayList<AttentionUser> listFrans(int uid, int page, int onepagecount) {
		ArrayList<AttentionUser> list = new ArrayList<AttentionUser>();
		String sql = "select * from t_attention as a inner join t_user as u on a.userid=u.id where a.AUserId=? limit ?,?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, (page - 1) * onepagecount);
			ps.setInt(3, onepagecount);
			
			
			rs = ps.executeQuery();
			
			while (rs.next()) {
				AttentionUser be = DBUtil.getJoinObject(rs, AttentionUser.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}


	public ArrayList<AttentionUser> listFrans(int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}


	public boolean isAttention(int uid, int auid) {
		String sql = "select * from t_attention where UserId=? and AUserId=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, uid);
			ps.setInt(2, auid);
			
			rs = ps.executeQuery();
			
			if (rs.next()){
				return true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	public boolean isFrans(int auid, int uid) {
		return false;
	}


	
	
	
	
}
